package com.sfilyh.servelt;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sfilyh.dao.ArticleDao;
import com.sfilyh.util.DBUtil;
import com.sfilyh.util.StringUtils;


@WebServlet("/article")
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		String cid =  request.getParameter("cid");
		request.setAttribute("cid", 1);		
		Integer category = null;
		if(StringUtils.validate(cid)){
			category = Integer.parseInt(cid);
			// 列表标签显示
			Map<String, Object> rMap = DBUtil.queryForMap("SELECT cname FROM tb_category WHERE id = ?", category);
			if(rMap != null && rMap.get("cname") != null){
				request.setAttribute("navTitle", rMap.get("cname"));
			}
		}
		
		String page =  request.getParameter("page");
		Integer p = null;
		if(StringUtils.validate(page)) p = Integer.parseInt(page);	    
	    
		ArticleDao  articleDao = new ArticleDao();
		
	    request.setAttribute("pb", articleDao.getAllList(p, 5, category, null ,new Integer[]{0}));
	    request.setAttribute("cid", cid);
	    request.getRequestDispatcher("/WEB-INF/views/article.jsp").forward(request, response);	        
	}	
}
